package cn.ondraw.ragflow.controller;

import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.common.core.domain.R;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.service.ConversationService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/v1/conversation")
public class ConversationController {
    private final ConversationService conversationService;

    /**
     * 设置会话信息
     *
     * @param request 设置会话的请求对象
     * @return 通用响应对象
     */
    @PostMapping("/set")
    public R<Object> setConversation(@RequestBody SetConversationRequest request) {
        try {
            return R.ok(conversationService.setConversation(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取会话信息
     *
     * @param conversation_id 会话 ID
     * @return 通用响应对象
     */
    @GetMapping("/get")
    public R<Object> getConversation(@RequestParam String conversation_id) {
        try {
            return R.ok(conversationService.getConversation(conversation_id));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 通过服务器端事件流获取会话信息
     *
     * @param dialog_id 对话 ID
     * @param token     授权令牌
     * @return 通用响应对象
     */
    @GetMapping("/getsse/{dialog_id}")
    public R<Object> getSseConversation(@PathVariable String dialog_id, @RequestHeader("Authorization") String token) {
        try {
            return R.ok(conversationService.getSseConversation(dialog_id, token.split(" ")[1]));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除会话信息
     *
     * @param conversation_ids 要删除的会话 ID 列表
     * @return 通用响应对象
     */
    @PostMapping("/rm")
    public R<Object> rmConversation(@RequestBody List<String> conversation_ids) {
        try {
            return R.ok(conversationService.rmConversation(conversation_ids));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 列出指定对话的会话列表
     *
     * @param dialog_id 对话 ID
     * @return 通用响应对象
     */
    @GetMapping("/list")
    public R<Object> listConversation(@RequestParam String dialog_id) {
        try {
            return R.ok(conversationService.listConversation(dialog_id));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 执行会话完成操作，将返回值统一为 CommonResponse 格式
     *
     * @param request 完成操作的请求对象
     * @return 通用响应对象
     */
    @PostMapping("/completion")
    public R<Object> completion(@RequestBody CompletionRequest request) {
        try {
            return R.ok(conversationService.completion(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 执行文本转语音操作，将返回值统一为 CommonResponse 格式
     *
     * @param request 文本转语音的请求对象
     * @return 通用响应对象
     */
    @PostMapping("/tts")
    public R<Object> tts(@RequestBody TtsRequest request) {
        try {
            return R.ok(conversationService.tts(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除会话消息
     *
     * @param request 删除消息的请求对象
     * @return 通用响应对象
     */
    @PostMapping("/delete_msg")
    public R<Object> deleteMsg(@RequestBody DeleteMsgRequest request) {
        try {
            return R.ok(conversationService.deleteMsg(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 对会话消息点赞
     *
     * @param request 点赞的请求对象
     * @return 通用响应对象
     */
    @PostMapping("/thumbup")
    public R<Object> thumbup(@RequestBody ThumbupRequest request) {
        try {
            return R.ok(conversationService.thumbup(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 发起会话提问，将返回值统一为 CommonResponse 格式
     *
     * @param request 提问的请求对象
     * @return 通用响应对象
     */
    @PostMapping("/ask")
    public R<Object> ask(@RequestBody AskRequest request) {
        try {
            return R.ok(conversationService.ask(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 生成会话思维导图
     *
     * @param request 生成思维导图的请求对象
     * @return 通用响应对象
     */
    @PostMapping("/mindmap")
    public R<Object> mindmap(@RequestBody MindmapRequest request) {
        try {
            return R.ok(conversationService.mindmap(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取相关问题列表
     *
     * @param request 获取相关问题的请求对象
     * @return 通用响应对象
     */
    @PostMapping("/related_questions")
    public R<Object> relatedQuestions(@RequestBody RelatedQuestionsRequest request) {
        try {
            return R.ok(conversationService.relatedQuestions(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
